package com.ruoyi.vote.admin.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.mall.enums.OrderInfoEnum;
import com.ruoyi.vote.admin.domain.VoteCertificateReceive;
import com.ruoyi.vote.admin.service.IVoteCertificateReceiveService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.vote.admin.domain.vo.VoteCertificateReceiveVo;
import com.ruoyi.vote.admin.domain.bo.VoteCertificateReceiveBo;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 证书申请
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vote/certificateReceive")
public class VoteCertificateReceiveController extends BaseController {

    private final IVoteCertificateReceiveService iVoteCertificateReceiveService;

    /**
     * 查询证书申请列表
     */
    @SaCheckPermission("vote:certificateReceive:list")
    @GetMapping("/list")
    public TableDataInfo<VoteCertificateReceiveVo> list(VoteCertificateReceiveBo bo, PageQuery pageQuery) {
        return iVoteCertificateReceiveService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取证书申请详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("vote:certificateReceive:query")
    @GetMapping("/{id}")
    public R<VoteCertificateReceiveVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iVoteCertificateReceiveService.queryById(id));
    }

    /**
     * 证书发货
     * @param id
     * @return
     */
    @GetMapping("/cert-delivery/{id}")
    public R<Void> certDelivery(@NotNull(message = "主键不能为空") @PathVariable Long id){
        return toAjax(iVoteCertificateReceiveService.update(new UpdateWrapper<VoteCertificateReceive>()
            .set("status", OrderInfoEnum.STATUS_2.getValue())
            .eq("id",id)));
    }

    /**
     * 新增证书申请
     */
    @SaCheckPermission("vote:certificateReceive:add")
    @Log(title = "证书申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VoteCertificateReceiveBo bo) {
        return toAjax(iVoteCertificateReceiveService.insertByBo(bo));
    }

    /**
     * 修改证书领取
     */
    @SaCheckPermission("vote:certificateReceive:edit")
    @Log(title = "证书申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VoteCertificateReceiveBo bo) {
        return toAjax(iVoteCertificateReceiveService.updateByBo(bo));
    }

    /**
     * 删除活动 - 证书申请
     *
     * @param ids 主键串
     */
    @SaCheckPermission("vote:certificateReceive:remove")
    @Log(title = "证书申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iVoteCertificateReceiveService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
